
import 'package:builder_ok/bean/login_data.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'dart:convert';
import 'Home.dart';
import 'package:lottie/lottie.dart';
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '建造师考试网',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: '建造师考试网'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var _username;

  var _password;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: Padding(
            padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
            child: Image.asset("images/app_icon.png"),
          ),
          title: Text(widget.title),
          actions: [
            Container(
              margin: EdgeInsets.only(right: 50),
              alignment: Alignment.center,
              child: TextButton(
                child: Text(
                  "小木屋科技有限公司",
                  style: TextStyle(fontSize: 13, color: Color(0xffe9efe7)),
                ),
                onPressed: () {
                  showToast("哈哈哈");
                },
              ),
            )
          ],
        ),
        body: Flex(
          direction: Axis.horizontal,
          children: [

            Expanded(
              flex: 1,
              child: Container(
                margin: EdgeInsets.only(top: 100),
                child: Stack(
                    children: [
                      Positioned(child: Image.asset("images/app_icon.png"),left: 100,width: 300,),
                      Positioned(child: Lottie.asset("json/tree.json"),right: 50,width: 350,)
                    ],)
              ),
            ),
            Container(
              width: 300,
              height: 300,
              margin: EdgeInsets.fromLTRB(100, 0, 200, 0),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(8),
                  boxShadow: [
                    BoxShadow(
                        color: Colors.black12,
                        offset: Offset(0, 15),
                        blurRadius: 15,
                        spreadRadius: 1)
                  ]),
              child: Column(
                children: [
                  Container(
                    margin: EdgeInsets.only(top: 16, bottom: 5),
                    child: Text(
                      "请登录",
                      style: TextStyle(color: Color(0xffffb849), fontSize: 16),
                    ),
                  ),
                  Divider(
                    color: Color(0xffffb849),
                    height: 1,
                  ),
                  Container(
                      margin: EdgeInsets.fromLTRB(20, 30, 20, 0),
                      height: 35,
                      width: 250,
                      decoration: BoxDecoration(
                          color: Color(0xfff5f5f5),
                          borderRadius: BorderRadius.circular(8)),
                      child: Container(
                        height: 35,
                        width: 250,
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.fromLTRB(8, 8, 12, 8),
                        child: TextField(
                          onChanged: (text) {
                            _username = text;
                          },
                          textAlign: TextAlign.justify,
                          style: TextStyle(fontSize: 12),
                          decoration: InputDecoration(
                              border: InputBorder.none,
                              icon: Image(
                                image: AssetImage('images/icon_login.png'),
                                height: 20,
                                width: 20,
                              ),
                              hintText: "请输入用户名"),
                        ),
                      )),
                  Container(
                    margin: EdgeInsets.fromLTRB(20, 30, 20, 0),
                    height: 35,
                    width: 250,
                    decoration: BoxDecoration(
                        color: Color(0xfff5f5f5),
                        borderRadius: BorderRadius.circular(8)),
                    child: Container(
                      padding: EdgeInsets.fromLTRB(8, 8, 12, 8),
                      height: 35,
                      width: 250,
                      child: TextField(
                        onChanged: (text) {
                          _password = text;
                        },
                        obscureText: true,
                        textAlign: TextAlign.justify,
                        style: TextStyle(fontSize: 12),
                        decoration: InputDecoration(
                            border: InputBorder.none,
                            icon: Image(
                              image: AssetImage('images/icon_password.png'),
                              height: 25,
                              width: 25,
                            ),
                            hintText: "请输入密码",
                            hintStyle: TextStyle(
                                fontStyle: FontStyle.normal, fontSize: 12)),
                      ),
                    ),
                  ),
                 Container(

                   margin: EdgeInsets.only(top: 35),


                      child:  MaterialButton(
                        onPressed: () {
                          _loginNet();
                        },
                        child: Container(
                          width: 200,
                          height: 40,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(20),
                              gradient: LinearGradient(colors: [
                                Color(0xffffb849),
                                Color(0xffff9c37),
                                Color(0xffffb849)
                              ])),
                          child: Center(
                            child: Text(
                              "登录",
                              style: TextStyle(color: Colors.white, fontSize: 16),
                            ),
                          )
                        ),


                    ),
                  )
                ],
              ),
            )
          ],
        ));
  }

  void _loginNet() async {
    var map = Map<String, dynamic>();
    map["username"] = _username;
    map["password"] = _password;

    var response  = await Dio()
        .post("https://www.godboy.cn/app/user/login", queryParameters: map);
    LoginData data = LoginData.fromJson(response.data);

    if (data.code == 200) {
      Navigator.of(context).push( MaterialPageRoute(builder: (context) => Home()));
    } else {
      showToast(response.data.message);
    }
    print(response);
  }
}
